package com.lcpay.download;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lcpay.download.core.AbstractDownExecute;
import com.lcpay.download.domain.DownResult;
import com.lcpay.download.interfaces.DownService;
import com.lcpay.download.utils.FuncUtils;

/**
 * 类HttpFile.java的实现描述：执行类
 * <p>
 * 外部只需要调用该类的download的方法
 * </p>
 * 
 * @author chenrs 2015年12月7日 下午11:34:19
 */
public class HttpFile extends AbstractDownExecute implements DownService {

    private static Logger       logger                     = LoggerFactory.getLogger(HttpFile.class);

    private static final String HTTP_DOWN_EXCETPION_CODE   = "9999";

    private static final String HTTP_DOWN_PARAM_CODE       = "2001";

    private static final String HTTP_DOWN_CREATE_FILE_CODE = "2003";

    private static final String HTTP_DOWN_NO_SPACE_CODE    = "2002";

    private static final String HTTP_DOWN_SUCCESS_CODE     = "0000";

    public HttpFile(String url, String saveurl) {
        super(saveurl, url);
    }

    public static void main(String[] args) {
        // 实例下载微信
        HttpFile httpFile = new HttpFile("http://dldir1.qq.com/weixin/Windows/WeChat1.5.exe",
                                         "G://monitor/WeChat1.5.exe");
        logger.info(httpFile.downloadDoc().toString());

    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getSaveurl() {
        return saveurl;
    }

    public void setSaveurl(String saveurl) {
        this.saveurl = saveurl;
    }

    @Override
    public DownResult downloadDoc() {

        long t = System.currentTimeMillis();
        // verify param
        DownResult downResult = new DownResult();
        if (FuncUtils.isNull(url) || FuncUtils.isNull(saveurl)) {
            downResult.setRetcode(HTTP_DOWN_PARAM_CODE);
            downResult.setRetmsg("param error");
            return downResult;
        }

        // 创建文件
        File f = new File(saveurl);

        if (f.isFile() && f.length() == getContentLength() && getContentLength() != -1) {
            downResult.setConsumeTime(String.valueOf(System.currentTimeMillis() - t));
            downResult.setRetcode(HTTP_DOWN_SUCCESS_CODE);
            downResult.setRetmsg("aready exists");
            downResult.setSaveurl(saveurl);
            return downResult;
        }

        if (!createFile()) {
            downResult.setRetcode(HTTP_DOWN_CREATE_FILE_CODE);
            downResult.setRetmsg("create file error");
            return downResult;
        }

        // 文件空间不足
        if (getContentLength() != -1 && f.getParentFile().getFreeSpace() < getContentLength()) {
            downResult.setRetcode(HTTP_DOWN_NO_SPACE_CODE);
            downResult.setRetmsg("disk not exists so much free space");
            return downResult;
        }

        // 开始下载
        try {
            super.startDwonload();
        } catch (IOException e) {
            logger.error("下载异常", e);
            downResult.setRetcode(HTTP_DOWN_EXCETPION_CODE);
            downResult.setRetmsg("download error");
            return downResult;
        }

        downResult.setConsumeTime(String.valueOf(System.currentTimeMillis() - t));
        downResult.setRetcode(HTTP_DOWN_SUCCESS_CODE);
        downResult.setRetmsg("success");
        downResult.setSaveurl(saveurl);
        return downResult;
    }

}
